package com.skyworth.skymanualview;
import com.skyworth.skymanual.R;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MetroView extends LinearLayout implements OnFocusChangeListener, OnGestureListener
{
    private final float SCAL_SIZE = 50;
    public static int HISTORY = 0;
    public static int LOCALBROWSER = 1;
    public static int SETTING = 2;
    public static int SOURCE = 3;
    public static int MYAPPS = 4;
    public static int TWO = 5;
    public static int UPDATE = 6;
    public static int TIANCI = 7;
    public static int COLLECT;
    private int CursorSpaceH = (int) (0);
    private int CursorSpaceW = (int) (0);
    private int x_position = 0, y_position = 0;
    protected int width = 0, height = 0;
    protected int w=0,h=0;
    private Context context;
    private SkyScaleAnimation acquireFocusAnim, releaseFocusAnim;
    protected int diaplayType = 0;
    protected int viewType=1;
    protected View content;
    protected String modelId = null;
    String mTarget = null;
    RelativeLayout bg_layout = null;

    String mPath = null;
    String mName = null;
    boolean isMusic = false;
    Intent mIntent = null;
    boolean isRUI = false;
    
    public final static int DETAIL_TYPE_UNKNOWN = -1;   
  
    public interface OnMouseFocusChangeListener
    {
        public void focuseChangedByMouse(int id);
    }

    public MetroView(Context context, int w, int h)
    {
        super(context);

        this.context = context;
        this.setFocusableInTouchMode(true);
        // this.setAlpha(60);
        this.setLongClickable(true);
        init(w, h);

    }
    public View findContentViewById(int id)

    {
        return content.findViewById(id);
    }

    private void init(int w, int h)
    {
        this.w=w;
        this.h=h;
        width = w + 2 * CursorSpaceW;
        height = h + 2 * CursorSpaceH;
        Log.i("lhy","!!!!!**width="+width+"   ;"+"height="+height);
        this.setPadding(CursorSpaceW, CursorSpaceH, CursorSpaceW, CursorSpaceH);
        this.setLayoutParams(new FrameLayout.LayoutParams(width, height));
        this.setFocusable(true);
        this.setGravity(Gravity.CENTER);
        this.setOnFocusChangeListener(this);

        float x_t = (width + SCAL_SIZE) / width;
        float y_t = (height + SCAL_SIZE) / height;      
        Log.v("zhoufeng'an", "focused   x_t   " + x_t + "  y_t  " + y_t);
        releaseFocusAnim = new SkyScaleAnimation(x_t, 1.0f, y_t, 1.0f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f, 50);
        Log.v("zhoufeng'an", "focused   x_t   " + x_t + "  y_t  " + y_t);
        acquireFocusAnim = new SkyScaleAnimation(1.0f, x_t, 1.0f, y_t, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f, 150);
    }

    public void changeDrawableToOrcle(){
    	
    }
    public void setViewContent(View v)
    {
    	 this.removeAllViews();
    	 
         bg_layout = null;
/*         if(viewType==1){
         	 bg_layout = new RelativeLayout(context);
         	 RelativeLayout.LayoutParams bg_params = new RelativeLayout.LayoutParams(
                      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
              bg_layout.setLayoutParams(bg_params);             
              bg_layout.setBackgroundResource(R.drawable.item_focus);
              int bh = bg_layout.getHeight();
              int bw = bg_layout.getWidth();
              Log.d("cdm","bg_h="+bh);
              Log.d("cdm","bg_w="+bw);
              bg_layout.setVisibility(View.INVISIBLE);
             
              Log.d("lhy", "--------------viewType----->>>>>>"+viewType);       
         }*/
         FrameLayout f_layout = new FrameLayout(context);
    	 RelativeLayout.LayoutParams f_params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                 LayoutParams.MATCH_PARENT);    
         f_params.addRule(RelativeLayout.CENTER_IN_PARENT);  
    	 int h = f_layout.getHeight();
         int w = f_layout.getWidth();
         Log.d("cdm","f_h="+h);
         Log.d("cdm","f_w="+w);
         f_layout.setLayoutParams(f_params);
         f_layout.removeAllViews();
         RelativeLayout.LayoutParams content_params = new RelativeLayout.LayoutParams(
        		 LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);       
         content = v;
         content_params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
         content.setLayoutParams(content_params);
         f_layout.addView(content);
         if(bg_layout!=null){
        	 f_layout.addView(bg_layout);
         }
         this.addView(f_layout);
    }
    public int getXPostion()
    {
        return x_position;
    }

    public void setXPosition(int x)
    {
        x_position = x;
    }

    public int getYPostion()
    {
        return y_position;
    }

    public void setYPosition(int y)
    {
        y_position = y;
    }

    public int getWidthSize()
    {
        return width;
    }

    public void setWidthSize(int w)
    {
        width = w;
    }

    public int getHeightSize()
    {
        return height;
    }

    public void setHeightSize(int h)
    {
        height = h;
    }

    public void unFocused()
    {
    	
       startAnimation(releaseFocusAnim.getAnimation());
      // bg_layout.setVisibility(View.INVISIBLE);
    }

    public void focused()
    {
    	Log.v("lhy", "focused  +++++++++++");
        startAnimation(acquireFocusAnim.getAnimation());
/*       if(bg_layout==null){
        	Log.e("lhy","~~~~~~~~~~~~~bg_layout=null*****");
        }else{
        	bg_layout.setVisibility(View.VISIBLE);
        }*/
        
        MetroView.this.bringToFront();
        
    }  
    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
        	
            focused();
            

        } else
        {
            v.setBackground(null);
            unFocused();
        }
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        //checkAndJump(type);
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        
        return true;
    }


    
}
